package com.czl.module_activity.viewmodel

import androidx.databinding.ObservableField
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.BulletinDetailBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriber200Helper
import com.czl.base.util.RxThreadHelper

class BulletinDetailViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
    var bulletinId:Int=0
    var bulletinDetail=ObservableField<BulletinDetailBean>(BulletinDetailBean("","","","",0,0,"",0,"",""))
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getBulletinDetail()
    })

    private fun getBulletinDetail() {
        model.getBulletinDetail(bulletinId)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<BulletinDetailBean>>(){
                override fun onResult(t: BaseBean<BulletinDetailBean>) {
                    if (t.data==null){
                        showNormalToast("无此公告！")
                    }else{
                        bulletinDetail.set(t.data)
                    }
                    onRefreshFinishEvent.postValue(true)
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    onRefreshFinishEvent.postValue(false)
                }

            })
    }

    val onRefreshFinishEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()

    fun getType(type:Int):String{
        return when(type){
            1->"物业公告"
            2->"便民服务"
            3->"版本更新"
            else->""
        }
    }
}